package com.jt9003.controller;

import com.jt9003.pojo.Archives;
import com.jt9003.service.Patient_recordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class Patient_recordsController {

    @Autowired
    private Patient_recordsService patient_recordsService;


    @RequestMapping("/getRecordslist")
    @ResponseBody
    public Object getRecordslist(@RequestParam(value = "p_name", required = false) String p_name,
                                 @RequestParam(value = "p_phone", required = false) String p_phone){

        Map map =new HashMap();
        Map param =new HashMap();
        System.out.println(p_name +p_phone+ "==============================");
        param.put("p_name", p_name);
        param.put("p_phone", p_phone);
        try {
            List<Archives> getRecordslist = patient_recordsService.getRecordslist(param);
            map.put("result", getRecordslist);
            map.put("success", true);
            map.put("message", "查询患者列表成功");
            map.put("code", "0");
            map.put("", "");
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", "查询患者列表失败");
        }
        return map;
    }



    @RequestMapping("/getRecordscount")
    @ResponseBody
    public Object getRecordscount(){
        Map map =new HashMap();
        try{
            int count  = patient_recordsService.getRecordscount();
            map.put("result", count);
            map.put("success", true);
            map.put("message", "查询患者档案数据成功");
        }catch (Exception e){
            map.put("success", false);
            map.put("message", "查询患者档案数据失败");
        }
        return map;
    }

    @RequestMapping("/delRecords")
    public Map delRecords(Integer p_id) {
        Map map =new HashMap();
        System.out.println("拿到的id是=========================" + p_id);
        try {
            int count = patient_recordsService.delRecords(p_id);
            if (count > 0) {
                map.put("message", "删除患者档案成功");
                map.put("success", true);
                map.put("result", count);
            } else {
                map.put("message", "删除患者档案失败");
                map.put("success", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "程序异常");
            map.put("success", false);
        }
        return map;
    }


    @RequestMapping("/upRecordsid")
    public Map<String, Object> upRecordsid(Archives archives) {
        Map<String, Object> map = new HashMap();
        try {
            int i = patient_recordsService.upRecordsid(archives);
            if (i > 0) {
                map.put("message", "修改患者档案成功");
                map.put("success", true);
            } else {
                map.put("message", "修改患者档案失败");
                map.put("success", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "程序异常");
            map.put("success", false);
        }
        return map;
    }



}
